﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BaoJian.Control.bj
{
    /// <summary>
    /// 搜索结果伴随的广告
    /// </summary>
    public partial class SearchResultAd : System.Web.UI.UserControl
    {
        string key = null;
        protected void Page_Load(object sender, EventArgs e)
        { 
            key = HttpContext.Current.Request.QueryString["skey"];
            if (key == null)
                return;
            if (!Page.IsPostBack)
            {
                BindData();
            }

        }
        private void BindData()
        {
            string s1 = "(";
            string s2 = "(";
            for (int i = 0; i < key.Length; i++)
            {
                if (i == 0)
                {
                    s1 += " NAME  like '%" + key[i] + "%' ";
                    s2 += " BaoJianGongNeng like '% " + key[i] + "%' ";
                }
                else
                {
                    s1 += "or NAME like '%" + key[i] + "%' ";
                    s2 += "or BaoJianGongNeng like '%" + key[i] + "%' ";
                }
            }
            s1 += " )";
            s2 += " )";

            //TODO:改变算法
            string sql = "SELECT top 4 pid,NAME,simg,BaoJianGongNeng FROM product WHERE " + s1 + " or " + s2;
            DataSet ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                ProductADList.DataSource = ds.Tables[0];
                ProductADList.DataBind();
            }
            else
            {
                string sql1 = "SELECT top 4 pid,NAME,simg,BaoJianGongNeng FROM product";
                ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql1);
                ProductADList.DataSource = ds.Tables[0];
                ProductADList.DataBind();
            }
        }
    }
}